<?php
/**
 * Created by PhpStorm.
 * User: david
 * Date: 4/6/19
 * Time: 6:51 PM
 */

namespace App\Transformers;


use App\Models\Batch;
use League\Fractal\TransformerAbstract;

class BatchTransformer extends TransformerAbstract
{
    protected $defaultIncludes = [
        'first_interview_num',
        'first_interview_jobs',
        'second_interview_num',
        'second_interview_jobs',
        'hr_interview_num',
        'hr_interview_jobs',
    ];

    public function transform(Batch $batch)
    {
        return [
            'id' => $batch->id,
            'name' => $batch->name,
            'time' => $batch->time,
            'time_format' => date('Y-m-d', $batch->time),
            'type' => $batch->type,
            'type_name' => Batch::TYPE_NAME[$batch->type],
            'group' => $batch->group,
            'interviewer' => $batch->interviewer,
            'created_at' => $batch->created_at ? $batch->created_at->toDateTimeString() : null,
            'updated_at' => $batch->updated_at ? $batch->updated_at->toDateTimeString() : null,
        ];
    }

    public function includeFirstInterviewNum(Batch $batch)
    {
        return $this->primitive($batch->firstInterview()->count());
    }

    public function includeFirstInterviewJobs(Batch $batch)
    {
        $lists = $batch->firstInterview;
        $arr = [];
        foreach ($lists as $list) {
            $arr[] = $list->recommend;
        }
        $arr = array_unique($arr);
        return $this->primitive(implode('、', $arr));
    }

    public function includeSecondInterviewNum(Batch $batch)
    {
        return $this->primitive($batch->secondInterview()->count());
    }

    public function includeSecondInterviewJobs(Batch $batch)
    {
        $lists = $batch->secondInterview;
        $arr = [];
        foreach ($lists as $list) {
            $arr[] = $list->recommend;
        }
        $arr = array_unique($arr);
        return $this->primitive(implode('、', $arr));
    }

    public function includeHrInterviewNum(Batch $batch)
    {
        return $this->primitive($batch->hrInterview()->count());
    }

    public function includeHrInterviewJobs(Batch $batch)
    {
        $lists = $batch->hrInterview;
        $arr = [];
        foreach ($lists as $list) {
            $arr[] = $list->recommend;
        }
        $arr = array_unique($arr);
        return $this->primitive(implode('、', $arr));
    }
}